package com.yuntongxun.ecdemo.voicechat.ui.activity.guide;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.ui.account.LoginActivity;
import com.yuntongxun.ecdemo.voicechat.ui.activity.RegisterFlagActivity;
import com.yuntongxun.ecdemo.voicechat.ui.activity.VoiceChatLoginActivity;
import com.yuntongxun.ecdemo.voicechat.ui.fragment.BaseVoiceChatFragment;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.yuntongxun.ecdemo.R.id.view;

/**
 */
public class GuideFourFragment extends BaseVoiceChatFragment {

    private static final String KEY_CONTENT = "GuideThreeFragment:position";
    @InjectView(R.id.guide_bg_iv)
    ImageView guideBgIv;

    @InjectView(R.id.register_btn)
    Button registerBtn;
    @InjectView(R.id.login_btn)
    Button loginBtn;

    private int mPosition;

    MediaPlayer mediaPlayer;
    private AssetManager assetManager;

    public static GuideFourFragment newInstance(int position) {
        GuideFourFragment fragment = new GuideFourFragment();
        fragment.mPosition = position;
        return fragment;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_CONTENT, mPosition);
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_guide_one;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mPosition = savedInstanceState.getInt(KEY_CONTENT);
        }

        mediaPlayer=new MediaPlayer();
        assetManager = mContext.getAssets();
    }

    @Override
    protected void onFirstUserVisible() {
        playMusic();
    }

    @Override
    protected void onUserVisible() {
//        playMusic();
    }

    private void playMusic(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }

        AssetFileDescriptor fileDescriptor = null;
        try {
            fileDescriptor = assetManager.openFd("sound/language_teaching_and_practicing.m4a");
            mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                    fileDescriptor.getStartOffset());

            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onUserInvisible() {
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if(mediaPlayer != null &&mediaPlayer.isPlaying() ){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @Override
    protected void initViewsAndEvents() {

        registerBtn.setVisibility(View.GONE);
        loginBtn.setVisibility(View.GONE);

        guideBgIv.setImageResource(R.drawable.guide_four);
    }

    @OnClick({R.id.register_btn, R.id.login_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_btn:
                startActivity(new Intent(mContext, RegisterFlagActivity.class));
                ((GuideTabActivity)mContext).finish();
                break;
            case R.id.login_btn:
                startActivity(new Intent(mContext, VoiceChatLoginActivity.class));
                ((GuideTabActivity)mContext).finish();
                break;
        }
    }
}
